{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "d9a6a981",
   "metadata": {},
   "outputs": [],
   "source": [
    "def func2 (x):\n",
    "    return x*x-2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "e600f10a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "3c062a8e",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "根号2的值大约为： 1.4142135623730947\n"
     ]
    }
   ],
   "source": [
    "i=0\n",
    "x=np.zeros((100,2))\n",
    "y=np.zeros((100,2))\n",
    "x[0,:]=([0,3])\n",
    "while True:\n",
    "    a=x[i,0]/2+x[i,1]/2\n",
    "    d=func2(a)\n",
    "    if d>0:\n",
    "        y[i,:]=[x[i,0],a]\n",
    "    if d<0:\n",
    "        y[i,:]=[a,x[i,1]]\n",
    "    x[i+1,:]=y[i,:]\n",
    "    if (x[i,1]-x[i,0])<0.000000000000001:\n",
    "        break\n",
    "    i=i+1\n",
    "print(\"根号2的值大约为：\",x[i,0])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
